package com.app.couponsharelife.ui.main.home.local

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.data.bean.LifeShopPageBean
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.viewmodel.LocalViewModel
import com.app.couponsharelife.utils.AppUtils
import com.app.fuquan.data.bean.PageStatus
import kotlinx.android.synthetic.main.activity_coupon_detail.*

class CouponDetailActivity : BaseActivity() {

    override val viewLayoutId: Int get() = R.layout.activity_coupon_detail

    private var mViewModel: LocalViewModel? = null

    private var couponId = ""

    private var mobile = ""

    override fun initViews() {
        if (intent.getStringExtra("couponId") != null)
            couponId = intent.getStringExtra("couponId")
        else {
            showToastMsg("数据错误，请退出重试")
            finish()
        }
        ivBack.setOnClickListener { finish() }
        tvGetCard.setOnClickListener {

        }
    }

    override fun initViewModel() {
        if (mViewModel == null) mViewModel = ViewModelProviders.of(this).get(LocalViewModel::class.java)
        if (!mViewModel!!.userCenter()!!.hasObservers()) {
            mViewModel!!.userCenter()!!.observe(this, Observer {
                if (it == null) {
                    showToastMsg("数据出错啦")
                    return@Observer
                }
                when (it.status) {
                    PageStatus.Loading -> {
                        showProgressDialog("请稍候")
                    }
                    PageStatus.Error -> {
                        mViewModel!!.freshUserCenter(hashMapOf(), false)
                        showToastMsg(parseErrorDate(it.error!!.message).tip)
                    }
                    PageStatus.Empty -> {
                        mViewModel!!.freshUserCenter(hashMapOf(), false)
                        showToastMsg("暂无数据")
                    }
                    PageStatus.Content -> {
                        mViewModel!!.freshUserCenter(hashMapOf(), false)
                        if (it.data!!.code == 200 && it.data.data != null) {
                            mobile = it.data.data!!.mobile
                            getLifeCardInfo(it.data.data!!.mobile)
                        } else {
                            showToastMsg("暂无优惠券信息")
                        }
                    }
                }
            })
            mViewModel!!.getLifeCardInfo()!!.observe(this, Observer {
                if (it == null) {
                    dismissProgressDialog()
                    showToastMsg("数据出错啦")
                    return@Observer
                }
                when (it.status) {
                    PageStatus.Loading -> {
                        showProgressDialog("请稍候")
                    }
                    PageStatus.Error -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetLifeCardInfo(hashMapOf(), false)
                        showToastMsg(parseErrorDate(it.error!!.message).tip)
                    }
                    PageStatus.Empty -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetLifeCardInfo(hashMapOf(), false)
                        showToastMsg("暂无数据")
                    }
                    PageStatus.Content -> {
                        dismissProgressDialog()
                        mViewModel!!.freshGetLifeCardInfo(hashMapOf(), false)
                        if (it.data!!.code == 200 && it.data.data != null) {
                            handleContent(it.data.data!!)
                        } else {
                            showToastMsg("暂无优惠券信息")
                        }
                    }
                }
            })
        }
        userCenter()
    }

    private fun userCenter() {
        val map = java.util.HashMap<String, String>()
        map["userId"] = AppUtils.getString(Contacts.USERID, "")
        mViewModel?.freshUserCenter(map, true)
    }

    private fun getLifeCardInfo(phone: String) {
        val map = HashMap<String, String>()
        map["cardId"] = couponId
        map["mobile"] = phone
        mViewModel?.freshGetLifeCardInfo(map, true)
    }

    private fun handleContent(data: LifeShopPageBean.CardsBean) {
        Glide.with(this).asBitmap()
                .apply(RequestOptions().circleCrop())
                .load(data.logo)
                .into(ivShopPic)
        tvShopName.text = data.brandName
        tvCouponInfo.text = data.title
        if (data.cardType == "CASH") {
            tvConditionTitle.visibility = View.VISIBLE
            tvCondition.visibility = View.VISIBLE
            tvCondition.text = data.condition
        }
        tvDeadline.text = data.usable
        if (data.isAllowToReceive!!) {
            tvGetCard.setBackgroundResource(R.drawable.act_local_shops_detail_buy_bg)
            tvGetCard.text = "领取到卡包"
        } else {
            tvGetCard.setBackgroundResource(R.drawable.act_local_shops_detail_no_buy_bg)
            tvGetCard.text = "券已领取至微信卡包"
        }
    }
}
